using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.esriSystem;
using System.Collections.Generic;

namespace CanSWAT
{
    /// <summary>
    /// Summary description for GenerateAdditionalInputsCommand.
    /// </summary>
    [Guid("f77cac5a-3a9e-4b41-a050-913934bbc948")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("CanSWAT.GenerateAdditionalInputsCommand")]
    public sealed class GenerateAdditionalInputsCommand : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IApplication m_application;
        public GenerateAdditionalInputsCommand()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "CanSWAT"; //localizable text
            base.m_caption = "Setup&Run";  //localizable text
            base.m_message = "Setup and run a CanSWAT model";  //localizable text 
            base.m_toolTip = "Setup and run a CanSWAT model";  //localizable text 
            base.m_name = "CanSWAT_SetupRunCanSWAT";   //unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")
        }

        #region Overriden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            m_application = hook as IApplication;
            _arcswatCmd = new Util.ArcMapCommands(m_application);

            //Disable if it is not ArcMap
            if (hook is IMxApplication)
                base.m_enabled = true;
            else
                base.m_enabled = false;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add GenerateAdditionalInputsCommand.OnClick implementation
            CanSWATSetupFrm frm = new CanSWATSetupFrm();
            frm.App = m_application;
            frm.SpatialAnalystExtentionAvailability = checkSpatialAnalystLicense();
            frm.onSetupSWATRun += new EventHandler(runArcSWATSetupSWATRun);
            frm.onReadSWATOutput += new EventHandler(runArcSWATReadSWATOutput);

            frm.ShowDialog();
        }

        private Util.ArcMapCommands _arcswatCmd = null;


        private void runArcSWATSetupSWATRun(object sender, EventArgs e)
        {
            _arcswatCmd.ArcSWAT_RunModel();
        }

        private void runArcSWATReadSWATOutput(object sender, EventArgs e)
        {
            _arcswatCmd.ArcSWAT_ExportOuput();
        }

        /// <summary>
        /// check out spatial analyst extension
        /// </summary>
        private bool checkSpatialAnalystLicense()
        {
            IExtension sa = m_application.FindExtensionByName("Spatial Analyst");
            IExtensionConfig extensionConfig = (IExtensionConfig)sa;
            if (!(extensionConfig.State == esriExtensionState.esriESUnavailable))
            {
                if (extensionConfig.State == esriExtensionState.esriESDisabled)
                    extensionConfig.State = esriExtensionState.esriESEnabled;

                return true;
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Spatial Analyst License is not available. The wind direction function can't be used.");
                return false;
            }
        }

        #endregion
    }
}
